package com.example.demo.auto.model;

import com.beiding.businessplatform.data.Vm;
import com.beiding.businessplatform.ql.QueryField;
import com.beiding.businessplatform.ql.QueryTable;
import com.example.demo.auto.entity.ABe;
import com.example.demo.auto.entity.BBe;
import com.example.demo.auto.entity.CBe;
import com.example.demo.auto.entityhelp.HelpRegeditImpl;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/*

    该对象和数据实体存在对应关系

    当给定了id以后,在保存时,首先从仓库中获取数据对象

    然后比对该对象相对仓库数据实体的变化

    数据关联问题

    A a =new A();

    a.setId();

    a.setBs(bList);//一旦调用了该方法就表示触发对B的关联修改

    //关联对象的修改有四种情形  无->有,有->无,无->无,有->有

    有->有需要进行讨论

    问题:
        jpa中对于实体的处理

        1. 感知不到值被删除



 */
public class A extends Vm {


    public A() {
        super(ABe.class, HelpRegeditImpl.INSTANCE.getHelp(ABe.class));
    }

    public String getName() {
        return (String) getValue("name");
    }

    //关联对象的变化
    public A setBs(List<B> bs) {
        set("bs", bs);
        return this;
    }

    public List<B> getBs() {
        return (List<B>) getVmList("bs", B.class);
    }

    public A setName(String name) {
        set("name", name);
        return this;
    }

    //TODO 当同纬度中具有多个B的时候才区分
    @Getter
    @Setter
    public static class B extends Vm {

        public B() {
            super(BBe.class, HelpRegeditImpl.INSTANCE.getHelp(BBe.class));
        }

        public B setName(String name) {
            set("name", name);
            return this;
        }

        public String getName() {
            return (String) getValue("name");
        }

        @Getter
        @Setter
        public static class C extends Vm {


            public C() {
                super(BBe.class, HelpRegeditImpl.INSTANCE.getHelp(CBe.class));
            }

            public C setName(String name) {
                set("name", name);
                return this;
            }

            public String getName() {
                return (String) getValue("name");
            }

            public static class FieldHolder {
                public final QueryField id;
                public final QueryField name;

                private FieldHolder(QueryTable parent) {
                    QueryTable _table = parent.childTable("c");
                    id = _table.makeField("id");
                    name = _table.makeField("name");
                }

            }

        }

        public static class FieldHolder {

            public final QueryField id;
            public final QueryField name;
            public final C.FieldHolder c;

            private FieldHolder(QueryTable parent) {
                QueryTable _table = parent.childTable("bs");
                id = _table.makeField("id");
                name = _table.makeField("name");
                c = new C.FieldHolder(_table);
            }

        }

    }


    //TODO 简单的行为

    public B bsNew() {
        return new B();
    }

    public void bsAdd(B b) {
        List<B> bs = getBs();
        if (bs == null) {
            bs = new ArrayList<>();
            setBs(bs);
        }
        bs.add(b);
    }

    public void bsRemove(B b) {
        List<B> bs = getBs();
        if (bs != null) {
            bs.remove(b);
        }
    }

    public B bsNewAdd() {
        B b = bsNew();
        bsAdd(b);
        return b;
    }


    private static final QueryTable _table = new QueryTable("A");
    public static final QueryField $allFields = _table.makeField("*");
    public static final QueryField id = _table.getIdField();
    public static final QueryField name = _table.makeField("name");
    public static final B.FieldHolder bs = new B.FieldHolder(_table);


}
